//
//  AppDelegate.swift
//  HMWeiBo
//
//  Created by Apple on 16/7/28.
//  Copyright © 2016年 itheima. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        // Override point for customization after application launch.
        
        FMDBManager.shareInstance
        
//        HMStatusListViewModel().readDBData(0, max_id: 0) { (data) -> () in
//            printLog(data)
//        }
        
//        sleep(2)
        
//        HMEmoticonManager().getEmoticon()
        
//       HMEmoticonManager().pageEmoticon(HMEmoticonManager.defaultEmoticon)
        
        print(NSHomeDirectory())

//        fatalError("提醒作用")
        
        // 设置一下根视图控制器
        
        // 初始化window
        window = UIWindow(frame: UIScreen.mainScreen().bounds)
        
        // 设置可见
        window?.makeKeyAndVisible()
        
        // 
        //  HMLoginViewModel.shareInstance.access_token != nil
        if  HMLoginViewModel.shareInstance.isLogin {
            
            // 登录成功了
            window?.rootViewController = HMWelcomeViewController()
        } else {
            
            // 没有登录
            window?.rootViewController = HMTabBarViewController()
        }
        
        // 监听通知
        
        // 注意方法: changeVC: :的意思是参数
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "changeVC:", name: kNotificationChangeViewController, object: nil)
        

        // 在程序一启动的时候就改变nav和tabbar的颜色
        setupAppreance()
        
        return true
    }

    //
    @objc private func changeVC(noti: NSNotification){
        
//        noti.object 用于区分是谁发的通知
        // is 的意思是 是不是这个类
        if noti.object is HMLoginViewController {
            
            window?.rootViewController = HMWelcomeViewController()
            
        } else {
            
            window?.rootViewController = HMTabBarViewController()
        }
        
    }
    
    /*
        AppDelegagte 怎么与 HMLoginVC / HMWelcomeVC 建立 一对一的关系
    
    */
    
    private func setupAppreance(){
       // 修改nav的渲染色
     UINavigationBar.appearance().tintColor = UIColor.orangeColor()
        
        // 可以把tabbar的渲染色拿过来
//        UITabBar.appearance().tintColor = UIColor.orangeColor()
    }
   
    // 移除通知 -- dealloc
    deinit {
        
        NSNotificationCenter.defaultCenter().removeObserver(self)
        
    }


}

